/*
 * =====================================================================================
 *
 *       Filename:  test.cpp
 *
 *    Description:  test
 *
 *        Version:  1.0
 *        Created:  2009年06月02日 14时33分16秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Ying Wang (WY), ywang@nlpr.ia.ac.cn
 *        Company:  Institute of Automation, Chinese Academy of Sciences
 *
 * =====================================================================================
 */
#include <iostream>
#include "ncvector.h"
#include "ncmatrix.h"
#include "statistical.h"
#include "blas.h"
#include "process.h"
#include "cholesky.h"
#include "imageio.h"
#include "kmeans.h"
#include "gmm.h"
#include "semigmm.h"
#include "symmeig.h"
#include "mixbern.h"
#include "logistic.h"
//#include "loopybp.h"
#include "imagerestore.h"
#include "plsa.h"
#include <QtGui/QImage>
#include <fstream>
typedef NCvector<double> Vec;
typedef NCmatrix<double> Mat;
typedef NCmatrix<int> Matint;
int main( int argc, char* argv[])
{
	Vec v(4,2.0);
	std::cout << v <<std::endl;
	Vec v2=v;
	std::cout << v2 << std::endl;
	Vec v3 = v+v2;
	std::cout << v3 << std::endl;
	std::cout << inner_product(v3,v2) <<std::endl;
	Mat m1(4,4,2.0);
	std::cout << m1 <<std::endl;
	Mat m2(m1);
	std::cout << m2 <<std::endl;
	m2[2][2] = 10;
	std::cout << m2 <<std::endl;
	m1[1][1] = m2[2][2];
	std::cout << m1 <<std::endl;
	Mat m3 = m2;
	std::cout<< m3 <<std::endl;
	m1 = m3;
	std::cout<< m1 <<std::endl;
	m1 += m3;
	std::cout<< m1 <<std::endl;
	m1 += 3;
	std::cout<< m1 <<std::endl;
	m1[0][2] = 40;
	std::cout<< m1 <<std::endl;
	std::cout<< m1[0][2] <<std::endl;
	Mat m4 = m1-m3;
	std::cout<< m4 <<std::endl;
	Mat m5 = m4*m1;
	std::cout<< m5 <<std::endl;
	Mat m6 = m5.transpose();
	std::cout<< m6 <<std::endl;
	Mat m7 = Mat::eye(5);
	std::cout<< m7 <<std::endl;
	Matint m8 = Matint::eye(5);
	std::cout<< m8 <<std::endl;
	Mat m9(5,5,1);
	m9[2][3]=100;
	std::cout<< m9 <<std::endl;
	Vec vv= mean(m9);
	std::cout<< vv <<std::endl;
	Mat m10 = cov(m9);
	std::cout<< m10 <<std::endl;
	std::cout<< m10*(1.0/5)<<std::endl;
	Mat m11(3,2,1);
	m11[0][0] = 0.1746;  m11[0][1] = -0.5883;
	m11[1][0] = -0.1867; m11[1][1] = 2.1832;
	m11[2][0] = 0.7258;  m11[2][1] = -0.1364;
	std::cout<<cov(m11)<<std::endl;
	std::cout<<"========"<<std::endl;
	Mat m12 = Mat::eye(4);
	Mat m13(m12*4);
	m13[0][0] = 0; m13[0][1] = 1; m13[0][2] = 2; m13[0][3] = 5;
	m13[1][0] = 1; m13[1][1] = 0; m13[1][2] = 3; m13[1][3] = 2.1;
	m13[2][0] = 1; m13[2][1] = 2; m13[2][2] = 1; m13[2][3] = 12;
	m13[3][0] = 7; m13[3][1] = 1.1; m13[3][2] =9; m13[3][3] = 23;


	std::cout<< m13 <<std::endl;
	Mat result = gaussjordan(m13,m12);
	std::cout << result <<std::endl;
	std::cout << m13 <<std::endl;
	std::cout << result*m13<<std::endl;

	Mat U,L;
	NCvector<int> indx = LUdcmp(m13,U);
	std::cout << U <<std::endl;
//	std::cout << L <<std::endl;
	std::cout << indx <<std::endl;
//	std::cout << L*U <<std::endl;
//
	Vec b(4,1.0);
	std::cout << b << std::endl;
	std::cout << solve(m13,b) <<std::endl;
	Mat B(4,2,1.0);
	std::cout << B << std::endl;
	std::cout << solve(m13,B) <<std::endl;

	Mat syms = m13.transpose() * m13;

	std::cout<< syms <<std::endl;
	std::cout<< choleskydcmp(syms) <<std::endl;

	std::cout<<gauss(5,5,2)<<std::endl;
	std::cout<<log(5,5,1)<<std::endl;
	std::cout <<gradientx(m13) <<std::endl;
	std::cout <<gradienty(m13) <<std::endl;

	QImage image("test.jpg");
	std::cout <<image.width() <<std::endl;
	std::cout <<image.bytesPerLine() <<std::endl;
	std::cout <<image.depth() <<std::endl;
	std::cout <<image.hasAlphaChannel() <<std::endl;

	MultiMat matrgb = imtomat(image);
	std::cout <<matrgb.size() <<std::endl;
	QImage newim = mattoim(matrgb[0]);
	newim.save("new.jpg");
	QImage newim1 = mattoim(matrgb[0],matrgb[1],matrgb[2]);
	newim1.save("new1.png");
	srand(time(0));
	for(int i=0;i<10;i++)
		std::cout<< randint(0,100) <<std::endl;
	std::ifstream fin("sattestindex.txt");
	NCvector<int> invec;
	fin>>invec;
	std::cout<< invec<<std::endl;
	std::ifstream fin1("km");
	NCmatrix<double> inmat;
	fin1>>inmat;
	std::cout<< inmat <<std::endl;
	NCmatrix<double> mean;
	//NCvector<int> assign = kmeans(inmat,2,mean);
	//std::cout<< assign<<std::endl;
	//std::cout<< mean <<std::endl;
	NCmatrix<double> sigma(2,2);
	sigma[0][0] = 1.7786; sigma[0][1]=-0.0528;
	sigma[1][0] = -0.0528;sigma[1][1]=0.5312;
	NCvector<double> mm(2);
	mm[0] = 1; mm[1] = 1;
	//std::cout<<loggausspdf(mm,mm,sigma)<<std::endl;




	std::ifstream fin2("gmm1.txt");
	NCmatrix<double> gmmdata;
	NCmatrix<double> kkm;
	fin2>>gmmdata;
	//std::cout<<kmeans(gmmdata,2,kkm)<<std::endl;
	std::cout<< kkm <<std::endl;
	std::cout<< gmmdata.row()<<std::endl;
	GMM gmm(gmmdata,2,Full);
	gmm.fit();

	std::cout<<gmm.means<<std::endl;
	std::cout<<gmm.covs[0]<<std::endl;
	//std::cout<<gmm.resp<<std::endl;
	std::cout<<gmm.fracs<<std::endl;
	//Cholesky chole(gmm.covs[0]);

	std::cout<<gmm.covs[1]<<std::endl;
 	std::cout<<"*********"<<std::endl;
 	fin2.close();
 	fin1.close();
 	fin.close();
 	NCmatrix<double> matsym = m13*m13.transpose();
 	Symmeig eigmat(matsym);
	std::cout<<matsym<<std::endl;
	std::cout<<"------"<<std::endl;
	std::cout<<eigmat.d<<std::endl;
	std::cout<<eigmat.z<<std::endl;
	QImage noiseim("a.bmp");
	std::cout<<noiseim.width()<<std::endl;
	MultiMat matrgbnoise = imtomat(noiseim);
	std::cout<<matrgbnoise.size()<<std::endl;
	mattoim(matrgbnoise[0]).save("noise.jpg");

	ImageRestore imrestore(matrgbnoise[0],18.0,8,200);
	imrestore.fastmaxprodbp();
	mattoim(imrestore.labelmatrix()).save("re4.jpg");
	for(int i=0;i<10;i++)
		std::cout<<randint(0,1000)<<std::endl;

}

